package com.example.mykotlin.controller

import cn.dev33.satoken.annotation.SaCheckPermission
import com.example.mykotlin.base.result.PageParam
import com.example.mykotlin.base.util.except
import com.example.mykotlin.base.util.runService
import com.example.mykotlin.base.util.selectById
import com.example.mykotlin.model.entity.AuditLog
import com.mybatisflex.kotlin.extensions.db.paginate
import com.mybatisflex.kotlin.extensions.kproperty.eq
import com.mybatisflex.kotlin.extensions.wrapper.andAll
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.constraints.Min
import org.apache.commons.lang3.StringUtils
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@Tag(name = "审计日志表Controller", description = "相关接口")
@RestController
@Validated
@RequestMapping("/auditLog")
class AuditLogController {

    @GetMapping
    @Operation(summary = "根据主键查询审计日志", description = "返回审计日志AuditLog对象")
    @SaCheckPermission("GET_auditLog")
    fun getAuditLogsById(@RequestParam @Min(value = 1, message = "主键不能小于1") id: Long) =
        runService { selectById<AuditLog>(id) }

    @GetMapping("/inPage")
    @Operation(summary = "分页条件查询审计日志", description = "返回审计日志AuditLog对象列表")
    @SaCheckPermission("GET_auditLog_inPage")
    fun getAuditLogsInPage(auditLog: AuditLog, @Validated p: PageParam) = runService {
        paginate<AuditLog>(p.page()) {
            select(*except<AuditLog>(AuditLog::changeValue))
            andAll(
                (AuditLog::id eq auditLog.id).`when`(auditLog.id != null),
                (AuditLog::userId eq auditLog.userId).`when`(auditLog.userId != null),
                (AuditLog::username eq auditLog.username).`when`(StringUtils.isNotEmpty(auditLog.username)),
                (AuditLog::ipAddress eq auditLog.ipAddress).`when`(StringUtils.isNotEmpty(auditLog.ipAddress)),
                (AuditLog::serviceIpAddress eq auditLog.serviceIpAddress).`when`(StringUtils.isNotEmpty(auditLog.serviceIpAddress)),
                (AuditLog::apiId eq auditLog.apiId).`when`(StringUtils.isNotEmpty(auditLog.apiId)),
                (AuditLog::apiName eq auditLog.apiName).`when`(StringUtils.isNotEmpty(auditLog.apiName)),
            )
        }
    }


}